package com.wiim.nhapnhap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;

import com.google.android.gms.maps.model.LatLng;
import com.wiim.common.Utilities;
import com.wiim.interfaceapp.LocationCallBack;
import com.wiim.libs.WorldUtilities;
import com.wiim.ui.LoginActivity;

public class SplashScreen extends Activity {
	/** The b active event. */
	protected boolean bActiveEvent = true;
	/** The is active app. */
	private boolean isActiveApp = false;
	/** The i time display splash. */
	protected int iTimeDisplaySplash = 1000;
	public static String regId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen_layout);
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() // or
				// .detectAll()
				// for
				// all
				// detectable
				// problems
				.penaltyLog().build());

		// Thread for displaying the SplashScreen
		Thread splashThread = new Thread() {

			@Override
			public void run() {

				try {
					int iCount = 0;
					while ((SplashScreen.this.bActiveEvent && (iCount < SplashScreen.this.iTimeDisplaySplash)) || !SplashScreen.this.isActiveApp) {
						sleep(100);
						if (SplashScreen.this.bActiveEvent) {
							iCount += 100;
						}

					}
				} catch (InterruptedException e) {
					// do nothing
					e.printStackTrace();
				} finally {
					SplashScreen.this.finish();
					final Intent itTab = new Intent().setClass(SplashScreen.this.getApplicationContext(), LoginActivity.class);
//					Bundle bundle = new Bundle();
//					bundle.putInt("indexTab", 0);
//					itTab.putExtras(bundle);
					SplashScreen.this.startActivity(itTab);
				}
			}
		};
		splashThread.start();
		//setLocation();
		isActiveApp = true;
//		checkInternetConnection();
	}

	private void checkInternetConnection() {
		boolean avail = WorldUtilities.checkInternetAvailable(this);

		if (!avail) {
			WorldUtilities.showDialogCannotConnectToServer(this, getString(R.string.txt_warning), getString(R.string.msg_internet_not_avail_continue));

		} else {
			isActiveApp = true;
		}

	}
	public void setLocation() {
		Utilities.getGPSLocation(SplashScreen.this, locationCallback);
	}

	LocationCallBack locationCallback = new LocationCallBack() {
		@Override
		public void callback(LatLng position, String result) {
			Utilities.getGlobalVariable(SplashScreen.this).setLatlongDefault(position);
			Utilities.getGlobalVariable(SplashScreen.this).setAddressDefault(result);
			Log.v("HAI", "LogLat: "+position);
			isActiveApp = true;
		}
	};


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
